package com.kgcx.dbutils.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.utils.CollectionUtils;
import com.kgcx.dbutils.domain.Region;
import com.xiaoleilu.hutool.captcha.CaptchaUtil;
import com.xiaoleilu.hutool.captcha.LineCaptcha;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class CityUtils {
    private static final Logger LOG = LoggerFactory.getLogger(CityUtils.class);
    private static List<Region> citys;

    public static void initCitys() {
        try {
            URL resource = CityUtils.class.getResource("/gd-pc-code.json");
            if (null != resource) {
                String s = IOUtils.toString(resource, StandardCharsets.UTF_8);
                List list = JSON.parseObject(s, List.class);
                if (CollectionUtils.isNotEmpty(list)) {
                    citys = new ArrayList<>();
                    list.forEach(i -> {
                        Region region = JSON.parseObject(((JSONObject) i).toJSONString(), Region.class);
                        region.setId(adCodeToInt(region.getAdcode()));
                        region.setName(region.getCity());
                        citys.add(region);
                    });
                }
                LOG.info(JSONObject.toJSONString(citys));
            }
        } catch (IOException e) {
            LOG.error("读取城市数据失败", e);
        }
    }

    public static Integer adCodeToInt(String adCode) {
        int length = adCode.length();
        if (length >= 6) {
            if (adCode.endsWith("00")) {
                return Integer.valueOf(adCode.substring(0, 4));
            }
        }
        return Integer.valueOf(adCode);
    }


    public static List<Region> getCitys() {
        if (null == citys) {
            initCitys();
        }
        return citys;
    }


    public static Region getCityByName(String name) {
        return get(getCitys(), null, name);
    }


    public static Region getCity(String code) {
        return get(getCitys(), code, null);
    }


    public static Region getCity(Integer code) {
        return get(getCitys(), toStringCode2(code), null);
    }


    public static String toStringCode(Integer code) {
        return toStringCode(String.valueOf(code));

    }

    public static String toStringCode2(Integer code) {

        return toStringCode2(String.valueOf(code));

    }

    public static String toStringCode(String code) {
        String s = String.valueOf(code);
        if (s.length() < 6) {
            int length = s.length();
            for (int i = 0; i < 6 - length; i++) {
                s = s + "0";
            }
        }
        return s;
    }

    public static String toStringCode2(String code) {
        String s = String.valueOf(code);
        if (s.length() < 6) {
            int length = s.length();
            for (int i = 0; i < 6 - length; i++) {
                s = "0" + s;
            }
        }
        return s;
    }

    public static Integer toIntCode(String code) {
        return TypeUtils.castToInt(code);
    }


    private static Region get(List<Region> cities, String code, String name) {
        for (int i = 0; i < cities.size(); i++) {
            Region region = cities.get(i);
            String c = region.getAdcode();
            Integer id = region.getId();
            String n = region.getCity();
            if (c.equals(code)
                    || toIntCode(c).equals(NumberUtils.toInt(code))
                    || StringUtils.startsWith(n, name)
                    || id.equals(NumberUtils.toInt(code))) {
                return region;
            }
        }
        return null;
    }

    public static Long getCityId(RequestData rd) {
        Long cityId = rd.getLong("cityId");
        if (null == cityId &&
                StringUtils.isNotBlank(rd.getString("cityName"))) {
            cityId = Long.valueOf(CityUtils.getCityByName(rd.getString("cityName")).getId());
        }
        return cityId;
    }

    public static String getNamesByIds(String ids) {
        StringBuilder names = new StringBuilder();
        if (StringUtils.isNoneBlank(ids)) {
            String[] split = ids.split(",");
            if (ArrayUtils.isNotEmpty(split)) {
                for (int i = 0; i < split.length; i++) {
                    Region city = getCity(split[i]);
                    if (null != city) {
                        names.append(city.getCity());
                        if (i != split.length - 1) {
                            names.append(",");
                        }
                    }
                }
            }
        }
        return names.toString();
    }

    public static void main(String[] args) {
//        Region city = CityUtils.getCity("4301");
//        System.out.println(city);
//        Region city2 = CityUtils.getCityByName("长沙市");
//        System.out.println(city2);
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(600, 300);
        //lineCaptcha.createImage("w3uja2");
        lineCaptcha.write("d://a.jpg");
    }
}
